//
//  ChatViewController.m
//  IMChezhil
//
//  Created by zhangliucheng on 15/7/9.
//  Copyright (c) 2015年 CheZhil. All rights reserved.
//

#import "ChatViewController.h"
#import "UIViewController+DismissKeyboard.h"
#import "IMCore.h"
#import "IMUserMessage.h"
#import "IMTextMessage.h"
#import "IMMessageManager.h"
#import "IMDelegate.h"
#import "MCServiceTextCell.h"

static NSString *identifierServiceText = @"identifierServiceText";
static NSString *identifierServiceImage = @"identifierServiceImage";
static NSString *identifierUserText = @"identifierUserText";
static NSString *identifierUserImage = @"identifierUserImage";
static NSString *identifierUserVoice = @"identifierUserVoice";

@interface ChatViewController () <IMMessageReceiverDelegate, UITableViewDataSource, UITableViewDelegate>

@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UITextField *content;

@property (strong, nonatomic) NSMutableArray *data;

@end

@implementation ChatViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupForDismissKeyboard];
    
    // 注册键盘弹出和弹入的通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];
    
    UINib *serviceText = [UINib nibWithNibName:@"MCServiceTextCell" bundle:nil];
//    UINib *serviceImage = [UINib nibWithNibName:@"MCServiceImageCell" bundle:nil];
    UINib *userText = [UINib nibWithNibName:@"MCUserTextCell" bundle:nil];
//    UINib *userImage = [UINib nibWithNibName:@"MCUserImageCell" bundle:nil];
//    UINib *userVoice = [UINib nibWithNibName:@"MCUserVoiceCell" bundle:nil];
    
    [self.tableView registerNib:serviceText forCellReuseIdentifier:identifierServiceText];
//    [self.tableView registerNib:serviceImage forCellReuseIdentifier:identifierServiceImage];
    [self.tableView registerNib:userText forCellReuseIdentifier:identifierUserText];
//    [self.tableView registerNib:userImage forCellReuseIdentifier:identifierUserImage];
//    [self.tableView registerNib:userVoice forCellReuseIdentifier:identifierUserVoice];

    self.data = [[IMCore shareInstance] getRecentLocalMessages:20];
    
    self.tableView.dataSource = self;
    self.tableView.delegate = self;
    [self scrollToButtom];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [[IMCore shareInstance] registerMessageDelegate:self];
}

- (void)viewDidDisappear:(BOOL)animated {
    [super viewDidDisappear:animated];
    [[IMCore shareInstance] removeMessageDelegate];
}

- (IBAction)send:(id)sender {
    IMUserMessage *message = [[IMCore shareInstance] sendTextMessage:self.content.text];
    [self.data addObject:message];
    [self.tableView reloadData];
    [self scrollToButtom];
    self.content.text = nil;
}

- (void)scrollToButtom {
    NSInteger s = [self.tableView numberOfSections];
    if (s < 1) return;
    NSInteger r = [self.tableView numberOfRowsInSection:s - 1];
    if (r < 1) return;
    NSIndexPath *ip = [NSIndexPath indexPathForRow:r-1 inSection:s-1];
    [self.tableView scrollToRowAtIndexPath:ip atScrollPosition:UITableViewScrollPositionBottom animated:NO];
}

#pragma mark - IMMessageReceiverDelegate
- (void)receiveMessage:(IMUserMessage *)message {
    [self.data addObject:message];
    [self.tableView reloadData];
    [self scrollToButtom];
}

- (void)sendMessageResult:(IMUserMessage *)message {
    NSUInteger index = [self.data indexOfObject:message];
    if (NSNotFound != index) {
        [self.data setObject:message atIndexedSubscript:index];
    } else {
        [self.data addObject:message];
    }
    [self.tableView reloadData];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return [self.data count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    IMTextMessage *msg = [self.data objectAtIndex:indexPath.row];
    MCServiceTextCell *cell;
    if (msg.messageDirection == MessageOwnerReceive) {
        cell = [tableView dequeueReusableCellWithIdentifier:identifierServiceText];
    } else {
        cell = [tableView dequeueReusableCellWithIdentifier:identifierUserText];
    }
    cell.message = msg;
    return cell;
}

#pragma mark - 键盘弹出和弹入监听
NSTimeInterval animationDuration = 0.25;
UIViewAnimationCurve animationCurve = UIViewAnimationCurveLinear;
CGRect keyboardFrame;

- (void)keyboardWillShow:(NSNotification *)notification {
    NSDictionary* userInfo = [notification userInfo];
    [[userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] getValue:&animationCurve];
    [[userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] getValue:&animationDuration];
    [[userInfo objectForKey:UIKeyboardFrameEndUserInfoKey] getValue:&keyboardFrame];
    self.view.frame = CGRectOffset(self.view.frame, 0, -CGRectGetHeight(keyboardFrame));
}

- (void)keyboardWillHidden:(NSNotification*)notification {
    self.view.frame = CGRectOffset(self.view.frame, 0, CGRectGetHeight(keyboardFrame));
}
@end
